package org.capybara.coffeehouse.device;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.capybara.coffeehouse.event.Event;
import org.capybara.coffeehouse.event.EventListener;

/**
 * A device is a physical thing that commands operate on.  Like a light controller.
 * Most commands take a device or two in their constructor.
 * 
 * Devices send events to their registered listeners when something happens, or they
 * expose methods to affect their attached hardware.
 */
public abstract class Device {
	
	private List<EventListener> eventListeners = new ArrayList<>();
	
	private final Logger log = Logger.getLogger(Device.class.getName());
	
	/**
	 * Returns the name of the device.
	 * @return
	 */
	abstract public String getName();
	
	public void addEventListener(EventListener e) {
		eventListeners.add(e);
	}
	
	void fireEvent(Event e) {
		log.info("firing event to "+eventListeners.size()+" listeners.");
		for(EventListener listener : eventListeners) {
			listener.handleEvent(e);
		}
	}

}
